#pragma once 
#include"util.hpp"
#include<mutex>
#include<string>
#define CONFIG_FILE "./cloud.conf"
namespace cloud
{
  class Config
  {
    private:
      static Config* _instance;
      static std::mutex _mutex;
      int _hot_time;
      int _server_port;
      std::string _server_ip;
      std::string _download_prefix;
      std::string _packfile_suffix;
      std::string _pack_dir;
      std::string _back_dir;
      std::string _backup_file;
      Config()
      {
        ReadConfigFile();
      }
      bool ReadConfigFile()
      {
        Util::FileUtil fconf(CONFIG_FILE);
        std::string sconf;
        if(fconf.GetContent(&sconf) == false)
        {
          std::cout << "ReadCOnfigFile err " << std::endl;
          std::cout << "load CONFIG_FILE failed" <<std::endl;
          return false;
        }
        Json::Value jconf;
        if(Util::JsonUtil::UnSerialize(&jconf,sconf) == false)
        {
          std::cout << "ReadCOnfigFile err " << std::endl;
          std::cout << "UnSerialize failed" <<std::endl;
          return false;
        }
        _hot_time = jconf["hot_time"].asInt();
        _server_port = jconf["server_port"].asInt();
        _server_ip = jconf["server_ip"].asString();
        _download_prefix = jconf["download_prefix"].asString();
        _packfile_suffix = jconf["packfile_suffix"].asString();
        _pack_dir = jconf["pack_dir"].asString();
        _back_dir = jconf["back_dir"].asString();
        _backup_file = jconf["backup_file"].asString();
        return true;
      }
    public:
      static Config* GetInstance()
      {
        if(_instance == nullptr)
        {
          _mutex.lock();
          if(_instance == nullptr)
          {
            _instance = new Config(); 
          }
          _mutex.unlock();          
        }
        return _instance;
      }
      int GetHotTime()
      {
        return _hot_time;
      }
      int GetServerPort()
      {
        return _server_port;
      }
      std::string GetServerIp()
      {
        return _server_ip;
      }
      std::string GetDownloadPrefix()
      {
        return _download_prefix;
      }
      std::string GetPackDileSuffix()
      {
        return _packfile_suffix;
      }
      std::string GetPackDir()
      {
        return _pack_dir;
      }
      std::string GetBackDir()
      {
        return _back_dir;
      }
      std::string GetBackupFile()
      {
        return _backup_file;
      }
  };
  Config* Config::_instance = nullptr;
  std::mutex Config::_mutex;
}


